package carassius.DAL;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author siebz0r
 */
public class OptimalAquariumWaterConditionTable
		extends Table<OptimalAquariumWaterConditionRow, SimpleEntry<Integer, String>>
{
	private final String SQL_ADD = "INSERT INTO optimal_aquarium_water_condition VALUES ( ? , ? , ? , ? )",
			SQL_DELETE = "DELETE FROM optimal_aquarium_water_condition WHERE aquarium_id = ? AND water_condition = ?",
			SQL_DELETE_BY_AQUARIUM_ID = "DELETE FROM optimal_aquarium_water_condition WHERE aquarium_id = ?",
			SQL_DELETE_BY_CONDITION_NAME = "DELETE FROM optimal_aquarium_water_condition WHERE water_condition = ?",
			SQL_GET_ALL = "SELECT * FROM optimal_aquarium_water_condition",
			SQL_GET_BY_AQUARIUM_ID = "SELECT * FROM optimal_aquarium_water_condition WHERE aquarium_id = ?",
			SQL_GET_BY_CONDITION_NAME = "SELECT * FROM optimal_aquarium_water_condition WHERE condition = ?",
			SQL_GET_BY_AQUARIUM_ID_AND_CONDITION_NAME = "SELECT * FROM optimal_aquarium_water_condition WHERE aquarium_id = ? AND water_condition = ?";
	private boolean _allCached;

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	//<editor-fold defaultstate="collapsed" desc="SQL add">
	public OptimalAquariumWaterConditionRow add(AquariumRow aquariumRow,
												WaterConditionRow waterConditionRow,
												Double minValue, Double maxValue) throws SQLException
	{
		if (aquariumRow != null && waterConditionRow != null)
		{
			return add(aquariumRow.getId(), waterConditionRow.getName(), minValue, maxValue);
		}
		return null;
	}

	public OptimalAquariumWaterConditionRow add(Integer aquariumId,
												String waterCondition,
												Double minValue, Double maxValue) throws SQLException
	{
		SimpleEntry<Integer, String> keys = add(new Object[]
				{
					aquariumId, waterCondition, minValue, maxValue
				});
		if (keys != null)
		{
			return getByPrimaryKey(keys);
		}
		return null;
	}

	@Override
	public SimpleEntry<Integer, String> add(Object[] args) throws SQLException
	{
		SimpleEntry<Integer, String> out = null;
		if (args != null
				&& args.length >= 4
				&& args[0] instanceof Integer
				&& (Integer) args[0] >= 0
				&& args[1] instanceof String
				&& !((String) args[1]).isEmpty()
				&& (args[2] instanceof Double || args[3] instanceof Double))
		{
			out = new SimpleEntry<Integer, String>((Integer) args[0], (String) args[1]);
			PreparedStatement st = Database.prepareStatement(SQL_ADD);
			st.setInt(1, out.getKey());
			st.setString(2, out.getValue());
			st.setObject(3, args[2], Types.DOUBLE);
			st.setObject(4, args[3], Types.DOUBLE);

			try
			{
				synchronized (getRows())
				{
					if (st.executeUpdate() == 1)
					{
						_allCached = false;
						getByPrimaryKey(out);
						_allCached = true;
					}
					else
					{
						out = null;
					}
				}
			}
			catch (SQLException ex)
			{
				throw ex;
			}
			finally
			{
				st.close();
			}
		}
		return out;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="SQL delete">
	@Override
	public boolean delete(SimpleEntry<Integer, String> keys) throws SQLException
	{
		boolean result = false;
		if (keys != null
				&& keys.getKey() != null
				&& keys.getKey() >= 0
				&& keys.getValue() != null
				&& !keys.getValue().isEmpty())
		{
			PreparedStatement st = Database.prepareStatement(SQL_DELETE);
			st.setInt(1, keys.getKey());
			st.setString(2, keys.getValue());

			try
			{
				synchronized (getRows())
				{
					if (st.executeUpdate() == 1)
					{
						result = true;
						getRows().remove(keys);
					}
				}
			}
			catch (SQLException ex)
			{
				throw ex;
			}
			finally
			{
				st.close();
			}
		}
		return result;
	}

	public boolean deleteByAquariumID(int id) throws SQLException
	{
		boolean result = false;
		if (id >= 0)
		{
			PreparedStatement st = Database.prepareStatement(SQL_DELETE_BY_AQUARIUM_ID);
			st.setInt(1, id);

			try
			{
				synchronized (getRows())
				{
					ArrayList<OptimalAquariumWaterConditionRow> removeQueue = getByAquariumId(id);
					if (st.executeUpdate() == removeQueue.size())
					{
						result = true;
						for (OptimalAquariumWaterConditionRow optimalAquariumWaterConditionRow : removeQueue)
						{
							getRows().remove(optimalAquariumWaterConditionRow.getPrimaryKeys());
						}
					}
				}
			}
			catch (SQLException ex)
			{
				throw ex;
			}
			finally
			{
				st.close();
			}
		}
		return result;
	}

	public boolean deleteByCondition(String name) throws SQLException
	{
		boolean result = false;
		if (name != null && !name.trim().isEmpty())
		{
			PreparedStatement st = Database.prepareStatement(SQL_DELETE_BY_CONDITION_NAME);
			st.setString(1, name.trim());

			try
			{
				synchronized (getRows())
				{
					ArrayList<OptimalAquariumWaterConditionRow> removeQueue = getByConditionName(name);
					if (st.executeUpdate() == removeQueue.size())
					{
						result = true;
						for (OptimalAquariumWaterConditionRow optimalAquariumWaterConditionRow : removeQueue)
						{
							getRows().remove(optimalAquariumWaterConditionRow.getPrimaryKeys());
						}
					}
				}
			}
			catch (SQLException ex)
			{
				throw ex;
			}
			finally
			{
				st.close();
			}
		}
		return result;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="SQL get">
	@Override
	public Collection<OptimalAquariumWaterConditionRow> getAll() throws SQLException
	{
		PreparedStatement st = Database.prepareStatement(SQL_GET_ALL);

		synchronized (getRows())
		{
			if (!_allCached)
			{
				try
				{
					ResultSet query = st.executeQuery();

					while (query.next())
					{
						put(new OptimalAquariumWaterConditionRow(query));
					}
					_allCached = true;
				}
				catch (SQLException ex)
				{
					throw ex;
				}
				finally
				{
					st.close();
				}
			}
			return getRows().values();
		}
	}

	public ArrayList<OptimalAquariumWaterConditionRow> getByAquariumId(int ID) throws SQLException
	{
		ArrayList<OptimalAquariumWaterConditionRow> out = null;
		if (ID >= 0)
		{
			out = new ArrayList<OptimalAquariumWaterConditionRow>();
			PreparedStatement st = Database.prepareStatement(SQL_GET_BY_AQUARIUM_ID);
			st.setInt(1, ID);

			try
			{
				synchronized (getRows())
				{
					if (_allCached)
					{
						for (OptimalAquariumWaterConditionRow optimalAquariumWaterConditionRow : getRows().values())
						{
							if (optimalAquariumWaterConditionRow.getAquarium().getId() == ID)
							{
								out.add(optimalAquariumWaterConditionRow);
							}
						}
					}
					else
					{
						ResultSet query = st.executeQuery();

						while (query.next())
						{
							OptimalAquariumWaterConditionRow optimalAquariumWaterConditionRow = new OptimalAquariumWaterConditionRow(query);
							put(optimalAquariumWaterConditionRow);
							out.add(optimalAquariumWaterConditionRow);
						}
					}
				}
			}
			catch (SQLException ex)
			{
				throw ex;
			}
			finally
			{
				st.close();
			}
		}
		return out;
	}

	public ArrayList<OptimalAquariumWaterConditionRow> getByConditionName(
			String name) throws SQLException
	{
		ArrayList<OptimalAquariumWaterConditionRow> out = null;
		if (name != null && !name.trim().isEmpty())
		{
			out = new ArrayList<OptimalAquariumWaterConditionRow>();
			PreparedStatement st = Database.prepareStatement(SQL_GET_BY_CONDITION_NAME);
			st.setString(1, name.trim());

			try
			{
				synchronized (getRows())
				{
					if (_allCached)
					{
						for (OptimalAquariumWaterConditionRow optimalAquariumWaterConditionRow : getRows().values())
						{
							if (optimalAquariumWaterConditionRow.getCondition().getName().equals(name))
							{
								out.add(optimalAquariumWaterConditionRow);
							}
						}
					}
					else
					{
						ResultSet query = st.executeQuery();

						while (query.next())
						{
							OptimalAquariumWaterConditionRow optimalAquariumWaterConditionRow = new OptimalAquariumWaterConditionRow(query);
							put(optimalAquariumWaterConditionRow);
							out.add(optimalAquariumWaterConditionRow);
						}
					}
				}
			}
			catch (SQLException ex)
			{
				throw ex;
			}
			finally
			{
				st.close();
			}
		}
		return out;
	}

	@Override
	public OptimalAquariumWaterConditionRow getByPrimaryKey(
			SimpleEntry<Integer, String> keys) throws SQLException
	{
		OptimalAquariumWaterConditionRow row = null;
		if (keys != null
				&& keys.getKey() != null
				&& keys.getKey() >= 0
				&& keys.getValue() != null
				&& !keys.getValue().isEmpty())
		{
			synchronized (getRows())
			{
				row = getRows().get(keys);
				if (row != null || _allCached)
				{
					return row;
				}

				PreparedStatement st = Database.prepareStatement(SQL_GET_BY_AQUARIUM_ID_AND_CONDITION_NAME);
				st.setInt(1, keys.getKey());
				st.setString(2, keys.getValue());

				try
				{
					ResultSet query = st.executeQuery();

					if (query.next())
					{
						row = new OptimalAquariumWaterConditionRow(query);
						put(row);
					}
				}
				catch (SQLException ex)
				{
					throw ex;
				}
				finally
				{
					st.close();
				}
			}
		}
		return row;
	}
	//</editor-fold>
	//</editor-fold>
}
